package com.twu.library.web;

import com.twu.library.domain.model.Book;
import com.twu.library.domain.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class MainController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value ="/search", method = RequestMethod.POST)
    public ModelAndView searchBook(HttpServletRequest request,Model model) {
        String bookName = request.getParameter("book");

        List<Book> bookList = bookService.getTheBookMsg(bookName);
        model.addAttribute("books", bookList);
        return new ModelAndView("search","searchResult",model);
    }
    @RequestMapping(value ="")
    public String index() {

        return "index";
    }

}
